﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using ERP_RORZE_SYSTEM;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Business
{
    public partial class frm_ForecastPlan_Yoyaku : Form
    {
        private INISettings mySetting;
        private SqlConnection myConnection;

        public frm_ForecastPlan_Yoyaku()
        {
            InitializeComponent();
            mySetting = new INISettings();
            myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.buttonYoyaku.Enabled = false;
        }

        private void planCheck_CheckedChanged(object sender, EventArgs e)
        {
            CheckToEnableYoyaku();
        }       

        private void structureCheck_CheckedChanged(object sender, EventArgs e)
        {
            CheckToEnableYoyaku();
        }

        private void freeOrderingCheck_CheckedChanged(object sender, EventArgs e)
        {
            CheckToEnableYoyaku();
        }

        private void remainFreeOrderingCheck_CheckedChanged(object sender, EventArgs e)
        {
            CheckToEnableYoyaku();
        }

        // Check to Enable Yoyaku
        private void CheckToEnableYoyaku()
        {
            if (this.planCheck.Checked && this.structureCheck.Checked
                && this.freeOrderingCheck.Checked && this.remainFreeOrderingCheck.Checked)
            {
                this.buttonYoyaku.Enabled = true;
            }
            else
            {
                this.buttonYoyaku.Enabled = false;
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void buttonYoyaku_Click(object sender, EventArgs e)
        {
            string error = null;

            bool noStructure = false;
            noStructure = CheckStructure();
            if (noStructure)
            {
                error = "There are some Robot have no structure. Please check your plan.";
            }

            if (string.IsNullOrEmpty(error))
            {

            }
        }

        ////////////////////////////////////////////////////
        /// Check if there are some Robot have no structure
        ///////////////////////////////////////////////////
        private bool CheckStructure()
        {
            bool ret = false;

            try
            {
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT t1.[PlanNo], t1.[RobotNo], t2.[PartNo], t2.[Quantity]"
                    + " FROM [241_Plan] AS t1 LEFT JOIN"
                    + " [222_Kosei] AS t2 ON t1.[RobotNo]=t2.[CompID]";                    
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    if (dataReader["PartNo"] == DBNull.Value)
                    {
                        ret = true;
                    }
                }
                dataReader.Close();
                cmd_sql.Dispose();
            }
            catch
            {
                ret = true;
            } 

            return ret;
        }
    }
}
